﻿using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace template_dll
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public SqlConnection _con;
        private DataTable dt = new DataTable();
        private DataTable dt_b = new DataTable();

        private void Form2_Load(object sender, EventArgs e)
        {
            BrandSel();
        }

        private void SubsSel(int barandId)
        {
            try
            {
                SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[1];
                par[0].name = "@brandid";
                par[0].value = barandId;
                dt.Clear();
                dataGridView2.DataSource = null;
                dt = new SqlStorProc().SqlProcExecWithQueryOneTable(_con, "pas_DMBrandSubscrSel", 10, par);
                dataGridView2.DataSource = dt.DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow.Index > -1)
            {
                Form3 f3 = new Form3();
                f3.formText = "Вставка новой подписки";
                f3.butText = "Сохранить";
                f3.brandText = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                if (f3.ShowDialog() == DialogResult.OK)
                {
                    SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[3];
                    par[0].name = "@brandid";
                    par[0].value = dataGridView1.CurrentRow.Cells[0].Value;
                    par[1].name = "@datest";
                    par[1].value = f3.dtSt.ToString("yyyyMMdd");
                    par[2].name = "@dateend";
                    par[2].value = f3.dtEnd.ToString("yyyyMMdd");
                    new SqlStorProc().SqlProcExecNonQuery(_con, "pas_DMBrandSubscrIns", 10, par);
                    SubsSel((int)par[0].value);
                }
                f3.Close();
                f3 = null;
            }
            else MessageBox.Show("Выберите одного производителя");
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                SubsSel((int)dataGridView1[0, e.RowIndex].Value);
            }
            else
            {
                dataGridView2.DataSource = null;
                dt.Clear();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView2.CurrentRow.Index > -1)
            {
                Form3 f3 = new Form3();
                f3.formText = "Измененеие подписки";
                f3.butText = "Сохранить";
                f3.brandText = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                f3.dtSt = (DateTime)dataGridView2.CurrentRow.Cells[2].Value;
                f3.dtEnd = (DateTime)dataGridView2.CurrentRow.Cells[3].Value;
                if (f3.ShowDialog() == DialogResult.OK)
                {
                    SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[3];
                    par[0].name = "@subid";
                    par[0].value = dataGridView2.CurrentRow.Cells[0].Value;
                    par[1].name = "@datest";
                    par[1].value = f3.dtSt.ToString("yyyyMMdd");
                    par[2].name = "@dateend";
                    par[2].value = f3.dtEnd.ToString("yyyyMMdd");
                    new SqlStorProc().SqlProcExecNonQuery(_con, "pas_DMBrandSubscrUpd", 10, par);
                    SubsSel((int)dataGridView2.CurrentRow.Cells[1].Value);
                }
                f3.Close();
                f3 = null;
            }
            else MessageBox.Show("Выберите одну строку");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView2.CurrentRow.Index > -1)
            {
                Form3 f3 = new Form3();
                f3.formText = "Удаление подписки";
                f3.butText = "Удалить";
                f3.brandText = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                f3.dtSt = (DateTime)dataGridView2.CurrentRow.Cells[2].Value;
                f3.dtEnd = (DateTime)dataGridView2.CurrentRow.Cells[3].Value;
                if (f3.ShowDialog() == DialogResult.OK)
                {
                    SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[1];
                    par[0].name = "@subid";
                    par[0].value = dataGridView2.CurrentRow.Cells[0].Value;
                    
                    new SqlStorProc().SqlProcExecNonQuery(_con, "pas_DMBrandSubscrDel", 10, par);
                    SubsSel((int)dataGridView2.CurrentRow.Cells[1].Value);
                }
                f3.Close();
                f3 = null;
            }
            else MessageBox.Show("Выберите одну строку");
        }

        private void BrandSel()
        {
            try
            {
                dt_b = new SqlStorProc().SqlProcExecWithQueryOneTable(_con, "pas_DMBrandForMonitorProdSel", 10, new SqlStorProc.sp_params[0]);
                dataGridView1.DataSource = dt_b.DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            button1_Click(sender, new EventArgs());
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView2_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            button2_Click(sender, new EventArgs());
        }
    }
}
